﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.Data;
using CrumbPlus.Core.Crumb;

namespace CrumbPlus.UI
{
    public partial class FrmCrumbPlusUI : Form
    {
        private CrumbWorker _activeCrumbWorker;

        public CrumbWorker ActiveCrumbWorker
        {
            get { return _activeCrumbWorker; }
            set
            {
                _activeCrumbWorker = value;

                if (value != null)
                {
                    btnStartOrPause.Enabled = true;
                }
            }
        }

        public FrmCrumbPlusUI()
        {
            InitializeComponent();
        }

        private void Reset()
        {
            lblCrumbItemContent.Text = "点击此处添加新的任务。";
            lblStartTime.Text = "开始时间";
            lblHowLong.Text = "持续时间";

            _activeCrumbWorker = null;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            using (FrmCrumbInfoEditor editor = new FrmCrumbInfoEditor())
            {
                if (editor.ShowDialog() == DialogResult.OK)
                {
                    lblCrumbItemContent.Text = editor.Crumb.Name;

                    _activeCrumbWorker = new CrumbWorker(1000, editor.Crumb);
                    _activeCrumbWorker.Elasped += new EventHandler<System.Timers.ElapsedEventArgs>(_activeCrumbWorker_Elasped);
                    _activeCrumbWorker.StateChanged += new EventHandler<CrumbWorkerStateChangedEventArgs>(_activeCrumbWorker_StateChanged);

                    btnStartOrPause.Enabled = true;
                }
            }
        }

        private void _activeCrumbWorker_StateChanged(object sender, CrumbWorkerStateChangedEventArgs e)
        {
            if (e.State == CrumbWorkerState.Started)
            {
                btnNew.Enabled = false;
                btnStop.Enabled = true;
            }
            else if (e.State == CrumbWorkerState.Stopped)
            {
                btnStop.Enabled = false;
                btnStartOrPause.Tag = "0";
                btnStartOrPause.Text = "开始";
                btnStartOrPause.Enabled = true;
                btnNew.Enabled = true;

                Reset();
            }
        }

        private void _activeCrumbWorker_Elasped(object sender, System.Timers.ElapsedEventArgs e)
        {
            DoThreadSafe(delegate
            {
                TimeSpan howLong = (sender as CrumbWorker).HowLong;
                lblHowLong.Text = string.Format("{0}时:{1}分:{2}秒",
                    howLong.Hours,
                    howLong.Minutes,
                    howLong.Seconds);
            });
        }

        private void btnStartOrPause_Click(object sender, EventArgs e)
        {
            if (_activeCrumbWorker != null)
            {
                if (btnStartOrPause.Tag.ToString() == "0")
                {
                    btnStartOrPause.Tag = "1";
                    btnStartOrPause.Text = "暂停";

                    _activeCrumbWorker.Curmb.StartTime = DateTime.Now;
                    lblStartTime.Text = _activeCrumbWorker.Curmb.StartTime.ToString("HH:mm:ss");
                    _activeCrumbWorker.Start();
                }
                else if (btnStartOrPause.Tag.ToString() == "1")
                {
                    btnStartOrPause.Tag = "2";
                    btnStartOrPause.Text = "继续";
                    _activeCrumbWorker.Pause();
                }
                else if (btnStartOrPause.Tag.ToString() == "2")
                {
                    btnStartOrPause.Tag = "1";
                    btnStartOrPause.Text = "暂停";
                    _activeCrumbWorker.Continue();
                }
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (_activeCrumbWorker != null)
            {
                _activeCrumbWorker.Stop();

            }
        }

        private delegate void Procedure();

        private void DoThreadSafe(Procedure proc)
        {
            if (this.IsDisposed) { return; }

            try
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(proc);
                }
                else
                {
                    proc();
                }
            }
            catch { }
        }

        private void FrmCrumbPlusUI_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void lblCrumbItemContent_Click(object sender, EventArgs e)
        {

        }

        private void lblCrumbItemContent_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) { return; }

            if (_activeCrumbWorker != null)
            {
                using (FrmCrumbInfoEditor editor = new FrmCrumbInfoEditor())
                {
                    editor.Crumb = _activeCrumbWorker.Curmb;

                    if (editor.ShowDialog() == DialogResult.OK)
                    {
                        lblCrumbItemContent.Text = editor.Crumb.Name;
                    }
                }
            }
            else
            {
                btnNew.PerformClick();
            }
        }
    }
}
